/**
 * 头像窗口界面
 * @author none
 *
 */
class TEBResultWindowView extends eui.Component/*how.module.Window*/ {
    private bg: eui.Image;
    private light: eui.Image;
    private title: eui.Image;
    private hat: eui.Image;
    private nickname: eui.Label;
    private base: eui.Label;
    private times: eui.Label;
    private score: eui.Label;

    private groupBg0: eui.Group;
    private name0: eui.Label;
    private base0: eui.Label;
    private times0: eui.Label;
    private score0: eui.Label;
    private banker0: eui.Image;

    private groupBg1: eui.Group;
    private name1: eui.Label;
    private base1: eui.Label;
    private times1: eui.Label;
    private score1: eui.Label;
    private banker1: eui.Image;

    private groupBg2: eui.Group;
    private name2: eui.Label;
    private base2: eui.Label;
    private times2: eui.Label;
    private score2: eui.Label;
    private banker2: eui.Image;

    private groupBg3: eui.Group;
    private name3: eui.Label;
    private base3: eui.Label;
    private times3: eui.Label;
    private score3: eui.Label;
    private banker3: eui.Image;
    //进入下一轮
    private btnContinue: how.Button;//继续游戏
    //关闭游戏
    private btnClose: how.Button;
    //进入下一局
    private continueGroup: eui.Group;//
    private continueTimeTxt: eui.Label;//倒计时
    private continueTxt: eui.Label;//进入下一轮
    private continueString: string = "";
    private continueInterval: number = -1;
    private continueTime: number = 3;
    private round: number = 0;//下一轮
    private removeFlag: boolean = false;//点击背景是否消失标志
    private btnRvmFlag: boolean = false;//是否点击了继续按钮
    //提示
    private gameEndTip: eui.Label;//有玩家被请离房间，本轮游戏结束
    public constructor() {
        super();
        this.skinName = "TEBResultSkin";
        this.gameEndTip.text = TEBLanguage.gameEndTip;
        this.initEvent();
    }
    private initEvent() {
        this.btnContinue.addEventListener(egret.TouchEvent.TOUCH_TAP, this.restartGame, this);
        this.btnClose.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeRequest, this);
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.checkRemove, this);
    }
    public setInfo(data: Array<TEBResultData>, index: number, banker: number, endFlag: number, round: number) {
        if (data.length == 0 || !data[index]) return;
        var winFlag: boolean;
        this.btnClose["bg"].source = "";
        this.bg.source = "";
        this.title.source = "";
        if (data[index].score >= 0) {//输赢标志
            this.bg.source = "bar_over_win_bg_png";
            this.light.source = "bar_over_win_light_png";
            this.title.source = "bar_over_win_title_png";
            this.hat.source = "bar_over_win_hat_png";
            how.SoundManager.playEffect("bar_player_win_mp3");
            winFlag = true;
        } else {
            this.bg.source = "bar_over_lose_bg_png";
            this.light.source = "bar_over_lose_light_png";
            this.title.source = "bar_over_lose_title_png";
            this.hat.source = "bar_over_lose_hat_png";
            winFlag = false;
        }
        var temp = 0;
        for (var t = 0; t < 4; t++) {
            if (t != banker) {
                if (data[t].score >= 0) {
                    temp += data[t].times;
                }
                else {
                    temp -= data[t].times;
                }
            }
        }
        data[banker]["times"] = Math.abs(temp);
        for (var i = 0; i < 4; i++) {
            var info = data[i];
            this["name" + i].text = info.nickname;
            this["base" + i].text = base.Utils.formatCurrency(info.base);
            this["times" + i].text = info.times;
            if (info.score >= 0) {
                this["score" + i].text = "+" + base.Utils.formatCurrency(info.score);
            } else {
                this["score" + i].text = base.Utils.formatCurrency(info.score);
            }
            if (index == i) {
                this["groupBg" + i].textColor = "bar_over_txt_bg_0_png";
                this["name" + i].textColor = 0xFFDE00;
                this["base" + i].textColor = 0xFFDE00;
                this["times" + i].textColor = 0xFFDE00;
                this["score" + i].textColor = 0xFFDE00;
            } else {
                this["groupBg" + i].source = "bar_over_txt_bg_png";
                this["name" + i].textColor = 0xFFFFFF;
                this["base" + i].textColor = 0xFFFFFF;
                this["times" + i].textColor = 0xFFFFFF;
                this["score" + i].textColor = 0xFFFFFF;
            }
            if (banker == i) {
                this["banker" + i].visible = true;
            } else {
                this["banker" + i].visible = false;
            }
        }
        if (endFlag == 0) {
            this.btnContinue.visible = false;
            this.continueGroup.visible = false;
            this.gameEndTip.visible = false;
            this.removeFlag = false;
            this.btnClose.visible = false;
        } else {
            this.btnContinue.visible = true;
            this.continueGroup.visible = false;
            this.removeFlag = true;
            if (round == 5) {
                this.gameEndTip.visible = false;
            } else if (!AppData.getInstance().isGameHide) {
                this.gameEndTip.visible = true;
                egret.setTimeout(this.setTipOut, this, 2000);
            }
            if (winFlag) {
                this.btnClose["bg"].source = "bar_over_btn_win_close_png";
            } else {
                this.btnClose["bg"].source = "bar_over_btn_lose_close_png";
            }
            this.btnClose.visible = true;
        }
        this.btnRvmFlag = false;
    }
    public setTipOut() {
        this.gameEndTip.visible = false;
    }
    public readyNextGame(round: number, time: number = 5) {
        this.btnContinue.visible = false;
        this.continueGroup.visible = true;
        // this.continueString = how.StringUtils.format(TEBLanguage.gameStart, round);
        this.continueTxt.text = how.StringUtils.format(TEBLanguage.gameStart, round);
        this.continueTimeTxt.text = time + "S";
        if (this.continueInterval != -1) {
            egret.clearInterval(this.continueInterval);
            this.continueInterval = -1;
        }
        this.continueTime = time;
        this.continueInterval = egret.setInterval(this.setNextGameCountDown, this, 800);
    }
    private setNextGameCountDown() {
        this.continueTime--;
        if (this.continueTime == 0) {
            this.stopCountDown();
            this.closeRequest();
        } else {
            this.continueTimeTxt.text = this.continueTime + "S";
        }
    }
    private stopCountDown() {
        if (this.continueInterval != -1) {
            egret.clearInterval(this.continueInterval);
            this.continueInterval = -1;
        }
    }
    private restartGame() {
        this.btnRvmFlag = true;
        this.stopCountDown();
        // // this.report(TEBResultWindowModule.request.restartGame);
        this.closeRequest();
        D.App.table.sendReady();
    }
    private checkRemove() {
        if (this.removeFlag && !this.btnRvmFlag) {
            this.stopCountDown();
            this.closeRequest();
        }
    }
    public closeRequest() {
        // // this.report(TEBResultWindowModule.request.closeRequest);
        this.close();
    }
    public close() {
        if (this.continueInterval != -1) {
            egret.clearInterval(this.continueInterval);
            this.continueInterval = -1;
        }
        // super.close();
        if(this.parent) this.parent.removeChild(this);
    }
}